<div class="filter">
    <div class="f3 item">
        <input type="text" class="am-form-field" placeholder="用户名" />
    </div>

    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">查看</button>
    </div>
</div>

<table class="am-table am-table-bordered am-table-striped am-table-compact" id="result-table">
  <thead>
  <tr>
    <th width="120px">登录时间</th>
    <th width="100px">用户</th>
    <th width="80px">用户组</th>
    <th width="80px">连续次数</th>
    <th>IP</th>
    <th width="80px">来源</th>
    <th width="120px">域名</th>
  </tr>
  </thead>
  <tbody>
  </tbody>
</table>
<div class="zxf_pagediv"></div>

<script>
    $(".f1 input").datepicker({ format: 'yyyy-mm-dd' });
    $(".f2 input").datepicker({ format: 'yyyy-mm-dd' });
    Util.restorePageFilter();

    var _f = Util.queryTemplate($("#result-table tbody"), ".zxf_pagediv", $(".button button"), function(page, size, cb) {
        var data = {
            pageNum: page,
            pageSize: size,
        };
        var v = $(".f3 input").val().trim();
        if (v) data.uid = v;

        Util.savePageFilter([".f3 input"])
        startLoading();
        CMD.user_failure_query(data, function(r) {
            stopLoading();
            cb(r);
        });
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append("<tr><td colspan='7'>暂无数据</tr>");
            return;
        }

        var ips = [];
        for (var i in list) {
            var d = list[i];
            var tr = $("<tr></tr>");
            if (d.time)
                tr.append('<td>' + new Date(d.time).Format("yyyy-MM-dd HH:mm:ss") + '</td>');
            else
                tr.append('<td> </td>');
            tr.append("<td>" + (d.user.mobile || '') + '</td>');
            var v = Rights.roles['' + (d.grade || 0)] || '';
            if (d.grade == 0) v = '普通会员';
            else if (d.grade == 49) v = '代理';
            tr.append("<td>" + v + "</td>");

            tr.append("<td>" + (d.count || '') + '</td>');
            tr.append("<td class='ip'>" + (d.ip || '') + '</td>');
            tr.append("<td>" + (d.source || '') + '</td>');
            tr.append("<td>" + (d.url || '') + '</td>');
            table.append(tr);
            if (d.ip && ips.indexOf(d.ip) == -1) ips.push(d.ip);
        }

        var arr = $("td.uri");
        for (var i = 0; i < arr.length; i++) {
            registerPopover($(arr[i]), i);
        }

        for (var i in ips) registerIp(ips[i]);
    });

    function registerIp(ip) {
        CMD.ip_info(ip, function(r) {
            var arr = $("td.ip");
            for (var i = 0; i < arr.length; i++) {
                if ($(arr[i]).html() == ip) {
                    $(arr[i]).html(ip + "(" + r.addr + ")");
                }
            }
        });
    }

</script>
